/**
 * Created by dukes on 2015/10/15.
 */
class SignItemRender extends eui.ItemRenderer{
    constructor(){
        super();
        this.skinName = "pet.SignItemRenderSkin";
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);
    }

    private onTouch(evt:egret.TouchEvent){
        if( Model.todaySigned) return;
        var name = evt.target.name;
        if( name != null && name != ""){
            var idx = parseInt(name.match(/\d+/)[0]);
            idx =  this.itemIndex*4+1+idx;
            if( Model.totalSignCount == (idx - 1)){
                window["__sign"]();
                SignPanel.getInstance().signItem = this;
            }
        }
    }

    private updateIcon(signInfo:SignInfo,lab_name:eui.Label,icon:eui.Image){
        var good:GoodInfo;
        if( signInfo.diamond!=0){
            icon.source = GameConst.Res_Root + "resource/assets/items/diamond.png";
            lab_name.text = "钻石\n"+"x"+signInfo.diamond;
        }else if(signInfo.ornamentId != 0){
            var dec = Model.GetDecDateById(signInfo.ornamentId);
            if( dec.classify == 4){
                icon.source = GameConst.Res_Root + "resource/assets/decorates/"+signInfo.ornamentId+"-1.jpg";
            }else{
                icon.source = GameConst.Res_Root + "resource/assets/decorates/"+signInfo.ornamentId+".png";
            }
            lab_name.text =  dec.name+"\n"+"x1";
        }else if( Model.me.myType == Model.GOOD_TYPE_CAT){
            if( signInfo.catPropCount != 0){
                icon.source = GameConst.Res_Root + "resource/assets/items/" + signInfo.catPropId + ".png";
                good = Model.GetGoodById(signInfo.catPropId);
                lab_name.text = good.name+"\n"+"x"+signInfo.catPropCount;
            }else if( signInfo.catHair != 0){
                icon.source = GameConst.Res_Root + "resource/assets/items/cathair.png";
                lab_name.text = "猫毛\n"+"x"+signInfo.catHair;
            }
        }else if( Model.me.myType == Model.GOOD_TYPE_DOG){
            if( signInfo.dogPropCount != 0){
                icon.source = GameConst.Res_Root + "resource/assets/items/" + signInfo.dogPropId + ".png";
                good = Model.GetGoodById(signInfo.dogPropId);
                lab_name.text = good.name+"\n"+"x"+signInfo.dogPropCount;
            }else if( signInfo.dogHair != 0){
                icon.source = GameConst.Res_Root + "resource/assets/items/doghair.png";
                lab_name.text = "狗毛\n"+"x"+signInfo.dogHair;
            }
        }
    }
    private day0:number;
    private day1:number;
    private day2:number;
    private day3:number;
    public dataChanged(){
        var vipLevel = Model.me.vipLevel;
        var startDay:number = this.itemIndex*4+1;
        var maxSignDay = Model.totalSignCount;
        var signInfo:SignInfo;
        signInfo = Model.signInfos[startDay-1];
        this.img_signed_0.visible = maxSignDay >= startDay;
        this.img_get_0.visible = maxSignDay+1 == startDay && !Model.todaySigned;
        this.vip_0.visible = signInfo.rateVipLevel >= 1;
        if( this.vip_0.visible){
            this.vip_0.source = RES.getRes("VIP"+signInfo.rateVipLevel);
        }
        if(this.day0 != startDay){
            this.group_day_0.text =startDay+"";
            //GetUIAssetsNum(startDay,this.group_day_0,1.0,true,false,null,"day_");
            this.day0 = startDay;
            this.img_bg_0.source = RES.getRes("img_bg_"+signInfo.backgroud);
            this.updateIcon(signInfo,this.lab_name_0,this.img_icon_0);
            this.lab_name_0.textColor = signInfo.backgroud == 2 ? 0xffffff : 0x954f55;
        }

        startDay++;
        signInfo = Model.signInfos[startDay-1];
        this.img_signed_1.visible = maxSignDay >= startDay;
        this.img_get_1.visible = maxSignDay+1 == startDay  && !Model.todaySigned;
        this.vip_1.visible =  signInfo.rateVipLevel >= 1;
        if( this.vip_1.visible){
            this.vip_1.source = RES.getRes("VIP"+signInfo.rateVipLevel);
        }
        if(this.day1 != startDay){
            this.day1 = startDay;
            this.group_day_1.text =startDay+"";
            //GetUIAssetsNum(startDay,this.group_day_1,1.0,true,false,null,"day_");
            this.img_bg_1.source = RES.getRes("img_bg_"+signInfo.backgroud);
            this.updateIcon(signInfo,this.lab_name_1,this.img_icon_1);
            this.lab_name_1.textColor = signInfo.backgroud == 2 ? 0xffffff : 0x954f55;
        }

        startDay++;
        if( startDay > 30){
            this.img_get_2.visible = this.img_bg_2.visible = this.img_day_lab2.visible = this.group_day_2.visible = this.lab_name_2.visible = this.img_icon_2.visible = this.img_signed_2.visible = false;
        }else{
            signInfo = Model.signInfos[startDay-1];
            this.img_signed_2.visible = maxSignDay >= startDay;
            this.img_get_2.visible = maxSignDay+1 == startDay  && !Model.todaySigned;
            this.img_day_lab2.visible = this.group_day_2.visible = this.lab_name_2.visible = this.img_icon_2.visible = this.img_bg_2.visible = true;
            this.vip_2.visible =  signInfo.rateVipLevel >= 1;
            if( this.vip_2.visible){
                this.vip_2.source = RES.getRes("VIP"+signInfo.rateVipLevel);
            }
            if(this.day2 != startDay){
                this.day2 = startDay;
                this.group_day_2.text =startDay+"";
                //GetUIAssetsNum(startDay,this.group_day_2,1.0,true,false,null,"day_");
                this.img_bg_2.source = RES.getRes("img_bg_"+signInfo.backgroud);
                this.updateIcon(signInfo,this.lab_name_2,this.img_icon_2);
                this.lab_name_2.textColor = signInfo.backgroud == 2 ? 0xffffff : 0x954f55;
            }
        }

        startDay++;
        if( startDay > 30){
            this.img_get_3.visible = this.img_bg_3.visible = this.img_day_lab3.visible = this.group_day_3.visible = this.lab_name_3.visible = this.img_icon_3.visible = this.img_signed_3.visible = false;
        }else{
            signInfo = Model.signInfos[startDay-1];
            this.img_get_3.visible = maxSignDay+1 == startDay  && !Model.todaySigned;
            this.img_signed_3.visible = maxSignDay >= startDay;
            this.img_day_lab3.visible = this.group_day_3.visible = this.lab_name_3.visible = this.img_icon_3.visible = this.img_bg_3.visible = true;
            this.vip_3.visible =  signInfo.rateVipLevel >= 1;
            if( this.vip_3.visible){
                this.vip_3.source = RES.getRes("VIP"+signInfo.rateVipLevel);
            }
            if(this.day3 != startDay){
                this.day3 = startDay;
                this.group_day_3.text =startDay+"";
                //GetUIAssetsNum(startDay,this.group_day_3,1.0,true,false,null,"day_");
                this.img_bg_3.source = RES.getRes("img_bg_"+signInfo.backgroud);
                this.updateIcon(signInfo,this.lab_name_3,this.img_icon_3);
                this.lab_name_3.textColor = signInfo.backgroud == 2 ? 0xffffff : 0x954f55;
            }
        }
    }
    private img_bg_0:eui.Image;
    private img_bg_1:eui.Image;
    private img_bg_2:eui.Image;
    private img_bg_3:eui.Image;
    private vip_0:eui.Image;
    private vip_1:eui.Image;
    private vip_2:eui.Image;
    private vip_3:eui.Image;
    private img_day_lab0:eui.Image;
    private img_day_lab1:eui.Image;
    private img_day_lab2:eui.Image;
    private img_day_lab3:eui.Image;
    private img_icon_0:eui.Image;
    private img_icon_1:eui.Image;
    private img_icon_2:eui.Image;
    private img_icon_3:eui.Image;
    private lab_name_0:eui.Label;
    private lab_name_1:eui.Label;
    private lab_name_2:eui.Label;
    private lab_name_3:eui.Label;
    private img_signed_0:eui.Image;
    private img_signed_1:eui.Image;
    private img_signed_2:eui.Image;
    private img_signed_3:eui.Image;
    private img_get_0:eui.Image;
    private img_get_1:eui.Image;
    private img_get_2:eui.Image;
    private img_get_3:eui.Image;
    private group_day_0:eui.BitmapLabel;
    private group_day_1:eui.BitmapLabel;
    private group_day_2:eui.BitmapLabel;
    private group_day_3:eui.BitmapLabel;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        instance.name = name;
        if( name == "img_bg_0"){
            this.img_bg_0 = instance;
        } else if( name == "img_get_3"){
            this.img_get_3 = instance;
        }else if( name == "img_get_2"){
            this.img_get_2 = instance;
        }else if( name == "img_get_1"){
            this.img_get_1 = instance;
        }else if( name == "img_get_0"){
            this.img_get_0 = instance;
        }else if( name == "img_bg_1"){
            this.img_bg_1 = instance;
        }else if( name == "img_bg_2"){
            this.img_bg_2 = instance;
        }else if( name == "img_bg_3"){
            this.img_bg_3 = instance;
        }else if( name == "img_day_lab0"){
            this.img_day_lab0 = instance;
        }else if( name == "img_day_lab1"){
            this.img_day_lab1 = instance;
        }else if( name == "img_day_lab2"){
            this.img_day_lab2 = instance;
        }else if( name == "img_day_lab3"){
            this.img_day_lab3 = instance;
        }else if( name == "img_icon_0"){
            this.img_icon_0 = instance;
        }else if( name == "img_icon_1"){
            this.img_icon_1 = instance;
        }else if( name == "img_icon_2"){
            this.img_icon_2 = instance;
        }else if( name == "img_icon_3"){
            this.img_icon_3 = instance;
        }else if( name == "lab_name_0"){
            this.lab_name_0 = instance;
        }else if( name == "lab_name_1"){
            this.lab_name_1 = instance;
        }else if( name == "lab_name_2"){
            this.lab_name_2 = instance;
        }else if( name == "lab_name_3"){
            this.lab_name_3 = instance;
        }else if( name == "img_signed_0"){
            this.img_signed_0 = instance;
        }else if( name == "img_signed_1"){
            this.img_signed_1 = instance;
        }else if( name == "img_signed_2"){
            this.img_signed_2 = instance;
        }else if( name == "img_signed_3"){
            this.img_signed_3 = instance;
        }else if( name == "group_day_0"){
            this.group_day_0 = instance;
            this.group_day_0.textAlign = "center";
        }else if( name == "group_day_1"){
            this.group_day_1 = instance;
            this.group_day_1.textAlign = "center";
        }else if( name == "group_day_2"){
            this.group_day_2 = instance;
            this.group_day_2.textAlign = "center";
        }else if( name == "group_day_3"){
            this.group_day_3 = instance;
            this.group_day_3.textAlign = "center";
        }else if( name == "vip_0"){
            this.vip_0 = instance;
        }else if( name == "vip_1"){
            this.vip_1 = instance;
        }else if( name == "vip_2"){
            this.vip_2 = instance;
        }else if( name == "vip_3"){
            this.vip_3 = instance;
        }
    }
}